<?php
    class ARingCode extends CActiveRecord
    {
        public static function model($className = __CLASS__)
        {
            return parent::model ( $className );
        }
        // goi den bang can ket noi   
        public function tableName() 
        {                  
            return 'c_ring_code';
        }  

        public function getDataCode(){ 
            $str_sql = "";
            $sql = "SELECT ringId,code,telco FROM c_ring_code WHERE 1 ".$str_sql."";
            $connect = Yii::app()->db;
            $command = $connect->createCommand($sql);
            $rows = $command->queryAll();
            
            foreach($rows as $row){
                $data[$row["ringId"]][$row["telco"]][] = $row["code"]; 
            }
            return isset($data) ? $data : array();
        }
        
        public function UpdateCode($data_code, $id)
        {
            /* Xóa hết code cũ */    
            $sql = "DELETE FROM c_ring_code WHERE ringId=" . $id;
            $command = Yii::app()->db->createCommand($sql);            
            $result = $command->execute();            
            
            $sql = "INSERT INTO c_ring_code SET
                ringId=:ringId
                , telco=:telco
                , code=:code                
            ";
            $command = Yii::app()->db->createCommand($sql);
            foreach($data_code as $row){
                $command->bindValues($row);
                $result = $command->execute();
            }      

            return $result;
        }
        
        public function InsertCode($data_code)
        {
            $sql = "INSERT INTO c_ring_code SET
                ringId=:ringId
                , telco=:telco
                , code=:code
                , deadline=:deadline                
            ";
            $command = Yii::app()->db->createCommand($sql);
            foreach($data_code as $row){
                $command->bindValues($row);
                $command->execute();
            }
        }
    }
?>
